document.querySelector(".fanhui").onclick = function () {
    location.href = "./index.html"
}

// 搜索列表
let Oscech = document.getElementById('scech');
let seachbtn = document.getElementById('seach-btn');
function searchInput() {
    // 添加对空格和回车键的事件监听
    Oscech.addEventListener('keydown', function (event) {
        // 检查是否按下空格或者回车键（请注意，键盘码可能因浏览器而异）
        if (event.key === 'Enter') {
            // console.log(Oscech.value);
            // compare(Oscech.value)
            getdata(Oscech.value)
        }
    });
}

// 拿数据
function getdata(data) {
    ajaxUsePromise02({
        url: `http://localhost:3000/books`,
        method: "get",
    }).then(res => {
        // console.log(res);
        res.forEach(element => {
            // console.log(element.name);
            // compare(element.name)
            if (data == element.name) {
                let oUl = document.querySelector('.seach-box');
                let oLis = oUl.children
                oUl.innerHTML += `<li class='seach-tit' lay-on="test-count-down">${element.name}</li>`
                for (let i = 0; i < oLis.length; i++) {
                    oLis[i].innerHTML = element.name;
                    oLis[i].onclick = function () {
                        if (getCookie("userphone") && getCookie("userpass")) {
                            location.href = `./list.html?category=${element.id}`
                        } else {
                            undefcookie()
                        }
                    }
                }
            }
        });
    })
}

function undefcookie() {
    layui.use(function () {
        var layer = layui.layer;
        var util = layui.util;
        // 事件
        util.on('lay-on', {
            "test-count-down": function () {
                layer.alert('还未登陆,快去登录，五秒后转至登录页面', {
                    time: 5 * 1000,
                    success: function (layero, index) {
                        var timeNum = this.time / 1000, setText = function (start) {
                            layer.title('<span class="layui-font-red">' + (start ? timeNum : --timeNum) + '</span> 秒后自动关闭', index);
                        };
                        setText(!0);
                        this.timer = setInterval(setText, 1000);
                        if (timeNum <= 0) clearInterval(this.timer);
                    },
                    end: function () {
                        location.href = `./login.html`
                        removeCookie("userphone");
                        removeCookie("userpass");
                        removeCookie("province");
                        removeCookie("city");
                        removeCookie("detail");
                        removeCookie("paytime");
                        clearInterval(this.timer);
                    }
                });
            }
        })
    });
}



window.onload = function () {
    searchInput()
    // getdata();
}